package com.demo.controller;

import com.alibaba.excel.EasyExcel;
import com.demo.common.excel.listenter.ClueDataListener;
import com.demo.common.model.R;
import com.demo.entity.Clue;
import com.demo.service.ClueService;
import com.demo.vo.ClueVo;
import com.demo.vo.PieData;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 线索接口管理
 */
@RestController
@RequestMapping("/api/clue")
public class ClueController {
    @Autowired
    private ClueService clueService;
    /**
     * 分页查询线索列表
     */
    @GetMapping("/page")
    public R<PageInfo<ClueVo>> loadCluePage(@RequestParam Integer current,
                                            @RequestParam(required = false,defaultValue = "10") Integer pageSize){
        PageInfo<ClueVo> pageInfo = clueService.queryClueByPage(current,pageSize);
        return R.OK(pageInfo);
    }
    /**
     * 导入线索 excel
     */
    @PostMapping("/importExcel")
    public R<String> importClueExcel(MultipartFile file) throws IOException {
        // 一步到位，解决循环依赖问题  Convert data com.alibaba.excel.metadata.data.ReadCellData@dab7574c to class java.lang.Integer error
        EasyExcel.read(file.getInputStream(), Clue.class, new ClueDataListener(clueService)).sheet().doRead();
         return R.OK(null);
    }

    /**
     *添加线索
     */
    @PostMapping
    public R<String> saveClue(@RequestBody Clue clue){
        boolean result = clueService.save(clue);
        return result ? R.OK(null) : R.FAIL("添加线索失败");
    }
    /**
     * 根据标识查询线索信息
     */
    @GetMapping("{clueId}")
    public R<Clue> loadClueInfo(@PathVariable Integer clueId){
        Clue result = clueService.getById(clueId);
        return R.OK(result);
    }
    /**
     * 编辑线索内容
     */
    @PutMapping
    public R<String> modifyClue(@RequestBody Clue clue){
        boolean result = clueService.updateById(clue);
        return result ? R.OK(null) : R.FAIL("修改线索失败");
    }

    /**
     * 单个删除线索记录
     */
    @DeleteMapping("/{id}")
    public R<String> removeClueById(@PathVariable Integer id){
        boolean removed = clueService.removeById(id);
        return removed ? R.OK(null) :R.FAIL("删除线索失败！");
    }
    /**
     * 进行批量删除
     */
    @DeleteMapping("/batch/{idArr}")
    public R<String> removeClueByIds(@PathVariable List<Integer> idArr){
        boolean removeds = clueService.removeByIds(idArr);
        return removeds ? R.OK(null) :R.FAIL("删除线索失败！");
    }
    /**
     * 根据标识查询线索详情
     */
    @GetMapping("/detail/info/{id}")
    public R<ClueVo> loadClueDetailInfo(@PathVariable Integer id){
        ClueVo clueVo = clueService.queryClueDetailInfoById(id);
        return R.OK(clueVo);
    }
    /**
     * 查询线索跟踪饼图数据
     */
    @GetMapping("/noteway")
    public R<List<PieData>> loadNotWayList(){
        List<PieData> list = clueService.queryNoteWayList();
        return R.OK(list);
    }

    /**
     *线索转换客户失败
     */
    @PostMapping("/toCustomer/{clueId}")
    public R<String> transformToCustomer(@PathVariable Integer clueId){
        Boolean transform = clueService.transformToCustomer(clueId);
        return transform ? R.OK(null) : R.FAIL("转换客户失败！");
    }
}
